package com.freeze;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

public class TestOcr {

    //设置APPID/AK/SK
    public static final String APP_ID = "88723071";
    public static final String API_KEY = "JWlcJTEGtOWv87GgNPYCoMWg";
    public static final String SECRET_KEY = "T3y3Oh2Krp3dvDvvbfBV9UoXjR4up6h4";


    public static void main(String[] args) throws Exception{

       testBasicGeneralImg();
       //System.out.println("===========================");
       //testAccurateGeneralImg();

        //System.out.println("===================================");
        //testPdf();
        //testTable();

        //testCarCard();
        //System.out.println("==================================");
        //testPdf2();
    }

    /**
     * ocr 识别图片
     */
    public static void testBasicGeneralImg(){
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\log4j.properties");

        // 调用接口
        String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\123.jpg";

        //String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\0701_3.jpg";
        // String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\123.jpg";
        // String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\123.jpg";
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        System.out.println(res.toString(2));
    }
    public static void testAccurateGeneralImg(){
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\log4j.properties");

        // 调用接口
        String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\123.jpg";
        JSONObject res = client.accurateGeneral(path, new HashMap<String, String>());
        System.out.println(res.toString(2));
    }

    public static void testPdf() throws Exception{
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "d:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\log4j.properties");

        // 调用接口
        String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\pdf\\2021-D-18930FinalReport_1.pdf";


        byte[] bytes = convertPdfToByteArray(path);
        //JSONObject res = client.basicGeneral(bytes, new HashMap<String, String>());
        //JSONObject res = client.accurateGeneral(bytes, new HashMap<String, String>());
        HashMap<String,String> ops = new HashMap<>();
        ops.put("pdf_file_num","1");
        JSONObject res = client.basicAccurateGeneralPdf(bytes, ops);

        System.out.println(res.toString(2));
    }

    public static void testPdf2() throws Exception{
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "E:\\test-ocr-0629\\src\\main\\resources\\log4j.properties");

        // 调用接口
        String path = "E:\\test-ocr-0629\\src\\main\\resources\\pdf\\1.pdf";


        //byte[] bytes = convertPdfToByteArray(path);
        //JSONObject res = client.basicGeneral(bytes, new HashMap<String, String>());
        //JSONObject res = client.accurateGeneral(bytes, new HashMap<String, String>());
        HashMap<String,String> ops = new HashMap<>();
        ops.put("pdf_file_num","2");
        //ops.put("pdf_file",path);
        JSONObject res = client.basicAccurateGeneralPdf(path, ops);

        System.out.println(res.toString(2));
    }
    public static byte[] convertPdfToByteArray(String filePath) throws IOException {
        // 获取文件路径对象
        Path path = Paths.get(filePath);
        // 读取文件内容为 byte[]
        return Files.readAllBytes(path);
    }

    public static void testCarCard(){
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "d:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\log4j.properties");

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("multi_detect", "true");


        // 参数为本地图片路径
        String image = "d:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\chepai\\3.jpg";
        JSONObject res = client.plateLicense(image, options);
        System.out.println(res.toString(2));

        // 参数为本地图片二进制数组
        byte[] file = FileUtil.readBytes(image);
        // byte[] file = readImageFile(image);
        res = client.plateLicense(file, options);
        System.out.println(res.toString(2));
    }

    public static void testTable() throws Exception{
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "d:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\log4j.properties");

        // 调用接口
        String path = "D:\\1\\test-ocr\\test-ocr-0629\\src\\main\\resources\\img\\p1.png";


        byte[] bytes = convertPdfToByteArray(path);
        //JSONObject res = client.basicGeneral(bytes, new HashMap<String, String>());
        //JSONObject res = client.accurateGeneral(bytes, new HashMap<String, String>());
        HashMap<String,String> ops = new HashMap<>();
        //ops.put("pdf_file_num","1");
        //JSONObject res = client.basicAccurateGeneralPdf(bytes, ops);
        JSONObject res = client.tableRecognitionAsync(bytes, ops);
        System.out.println(JSONUtil.toJsonStr(res));

        System.out.println(res.toString(2));
    }


}
